package com.dw.article.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dw.apis.UserClient;
import com.dw.article.mapper.mp.CommentInfoMapper;
import com.dw.article.service.ArticleUserService;
import com.dw.article.service.CommentUserService;
import com.dw.article.utils.LoginUtil;
import com.dw.common.entitys.BaseResponse;
import com.dw.model.article.dtos.comment.AddCommentDTO;
import com.dw.model.article.entitys.CommentInfo;
import com.dw.article.service.CommentInfoService;
import com.dw.model.article.vos.article.GetCountVO;
import com.dw.model.article.vos.article.QueryByIdVO;
import com.dw.model.article.vos.article.QueryUserArticleVO;
import com.dw.model.article.vos.article.SearchArticleVO;
import com.dw.model.article.vos.comment.QueryCommentVO;
import com.dw.model.user.vos.GetUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论信息表(CommentInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-10-14 16:31:09
 */
@Service("commentInfoService")
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {

    @Resource
    private CommentUserService commentUserService;

    @Resource
    private UserClient userClient;

    @Override
    public Boolean add(AddCommentDTO addCommentDTO) {
        CommentInfo commentInfo = new CommentInfo();
        BeanUtils.copyProperties(addCommentDTO, commentInfo);
        // 设置评论id
        long l = IdUtil.getSnowflake().nextId();
        commentInfo.setLeiId(l);
        // 设置评论的创建人
        commentInfo.setCreatedBy(Long.valueOf(LoginUtil.getLoginId()));
        return save(commentInfo);
    }

    /**
     * 根据文章id查询评论
     */
    @Override
    public List<QueryCommentVO> queryComment(List<CommentInfo> commentInfoList) {
        return commentInfoList.stream().map(item -> {
            Long commentId = item.getLeiId();
            // 查询评论点赞数量
            QueryCommentVO queryCommentVO = new QueryCommentVO();
            Integer likeCount = commentUserService.getCommentLikeCount(commentId);
            // 拷贝基本数据
            BeanUtils.copyProperties(item, queryCommentVO);
            // 拷贝点赞数量
            queryCommentVO.setLikeCount(likeCount);
            // 获取登录用户与文章之间的点赞状态
            Integer like = commentUserService.isLike(commentId);
            queryCommentVO.setIsLiked(like);
            // 获取评论发布者的头像和昵称
            BaseResponse<GetUserVO> getUserVO = userClient.getUserName(item.getCreatedBy());
            queryCommentVO.setAvatar(getUserVO.getData().getAvatar());
            queryCommentVO.setNickName(getUserVO.getData().getNickname());
            // 将雪花算法生成的id转为字符串，便于前端接收
            queryCommentVO.setLeiId(String.valueOf(commentId));
            queryCommentVO.setCreatedBy(String.valueOf(item.getCreatedBy()));
            return queryCommentVO;
        }).collect(Collectors.toList());
    }

}

